<template>
  <div class="subpage">
      <div class="supply-detail-container">
            <div class="project-item-info">
                <div class="project-img">
                    <img :src="detail.smallPic" alt="">
                </div>
                <ul class="project-item-detail">
                    <li>
                        <p class="project-name">{{detail.productName}}({{detail.farmName}}）</p>
                    </li>
                    <li>
                        <span>养殖地：{{detail.cattleAddress}}</span>
                    </li>
                    <li>
                        <span>重量：{{detail.productWeight}}{{detail.productWeightUnit}}</span>
                        <span>单价：{{detail.salesPrice}}{{detail.salesPriceUnit}}</span>
                    </li>
                    <li>
                        <p class="project-price">
                            <span>{{detail.totalPrice}}</span>{{detail.productPriceUnit}}
                        </p>
                        <p>
                            交货期：<span class="color-primary">{{detail.deliveryTime}}</span>个月
                        </p>
                    </li>
                </ul>
            </div>

            <div class="supply-goods-box">
                <ul class="supply-goods-top">
                    <!-- <li class="supply-goods-top-item">
                        <p>我要供货</p>
                        <div class="supply-goods-choose">
                            <van-stepper v-model="supplyValue" integer min="1" max="10" @plus="addAction" @minus="minusdAction" />
                        </div>
                    </li> -->
                    <li>
                        <p>求购数量</p>
                        <p>{{applyNum}}头</p>
                    </li>
                    <li>
                        <p>总重量</p>
                        <p>{{applyNum * detail.productWeight || ''}}kg</p>
                    </li>
                    <li>
                        <p>总金额</p>
                        <p class="color-primary">{{applyNum * detail.totalPrice || 0}}元</p>
                    </li>
                </ul>
                <div class="supply-goods-bottom">
                    <div class="supply-goods-bottom-title">选择对应货品</div>
                    <div class="supply-goods-list" v-if="detail.supplyOrderDetailVos && detail.isMe == 0">
                        <ul class="supply-goods-list-top">
                            <li>品名</li>
                            <li>订单号</li>
                            <li>数量</li>
                            <li></li>
                        </ul>
                        <ul class="supply-goods-list-bottom">
                            <li v-for="(item, index) in detail.supplyOrderDetailVos" :key="index">
                                <div class="goods-name">{{item.productName}}</div>
                                <div class="goods-no">{{item.outTradeNo}}</div>
                                <div class="goods-quantity">{{item.quantity}}头</div>
                                <div class="goods-checked" @click="chooseProduct(index)">
                                    <img src="../../assets/img/checked_pre_icon.png" alt="" v-if="item.checkFlag">
                                    <img src="../../assets/img/checked_icon.png" alt="" v-else>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <p class="no-data-text" v-else>{{detail.isMe == 0 ? '暂无对应货品可选~' : detail.isMe == 1 ? '不可对自己的求购订单供货~' : ''}}</p>
                </div>
            </div>

            <div class="supply-agreement-box" v-if="detail.supplyOrderDetailVos && detail.isMe == 0">
                <van-icon :name="checkedName" :class="checkedClass" @click="agreeAgreement" />
                <p>阅读且同意<span class="color-primary" @click="jumpAgreement">《友牧农场服务协议》</span></p>
            </div>
      </div>

      <div class="supply-detail-bottom" v-if="detail.supplyOrderDetailVos && detail.isMe == 0">
          <div class="supply-detail-bottom-left">
              <span>共{{applyNum}}头</span>
              <span>总计：{{applyNum * detail.totalPrice}}元</span>
          </div>
          <van-button round class="u-btn-default u-bg-gradient-light sure-btn" @click="supplyFn">确认供货</van-button>
      </div>
  </div>
</template>

<script>
import { Dialog } from 'vant';
export default {
    components:{
        [Dialog.name]:Dialog
    },
    data(){
        return{
            detail: {},
            supplyValue: 1,
            checked: true,
            applyNum: '',
            checkedClass: 'color-gray checked-icon',
            checkedName: 'circle',
            agreeFlag: false,
            orderDetailId: '',
            wantCattleId: '',
        }
    },
    created() {
        this.applyNum = this.$route.query.applyNum
        this.getDetail()
    },
    methods:{
        addAction(){ //增加
            ++this.supplyValue
        },
        minusdAction(){ //减少
            --this.supplyValue
        },
        getDetail() { //获取详情
            this.$api.get('/api/supply/detial/' + this.$route.query.id)
                .then((res) => {
                    if(res.data.supplyOrderDetailVos){
                        res.data.supplyOrderDetailVos.forEach((item) => {
                            item.checkFlag = false;
                        })
                    }
                    this.detail = res.data;
                })
        },
        chooseProduct(index) { //选择订单
            this.detail.supplyOrderDetailVos.forEach((item) => {
                item.checkFlag = false;
            })
            if(this.detail.supplyOrderDetailVos[index].checkFlag){
                this.detail.supplyOrderDetailVos[index].checkFlag = false;
            }else{
                this.detail.supplyOrderDetailVos[index].checkFlag = true;
                this.orderDetailId = this.detail.supplyOrderDetailVos[index].orderDetailId;
                if(this.detail.supplyOrderDetailVos[index].quantity > this.applyNum){
                    Dialog.alert({
                        title: '提示',
                        message: `将从当前勾选货品中供应${this.applyNum}头给求购者`
                    }).then(() => {
                        
                    });
                }
            }
        },
        supplyFn() { //供货
            let chooseArr = [];
            let supplyOrderDetailVos = this.detail.supplyOrderDetailVos;
            for(let i = 0; i < supplyOrderDetailVos.length; i++){
                chooseArr.push(supplyOrderDetailVos[i].checkFlag)
            }
            if(!chooseArr.includes(true)){
                this.$tool.toast('请选择对应货品');
                return;
            }
            if(!this.agreeFlag){
                this.$tool.toast('请阅读并同意友牧农场服务协议');
                return;
            }

            this.supplyAjax()
        },
        supplyAjax() {
            let postData = {
                orderDetailId: this.orderDetailId,  //供牛订单id
                wantCattleId: this.$route.query.id,    //求购id
                quantity: this.applyNum             //数量
            }
            this.$api.post('/api/supply/save', postData)
                .then((res) => {
                    this.$router.push({
                        name: 'supplyState',
                        query: {
                            totalWeight: this.applyNum * this.detail.productWeight,
                            salesPrice: this.detail.salesPrice,
                            salesPriceUnit: encodeURI(this.detail.salesPriceUnit),
                            orderId: res.data
                        }
                    })
                })
        },
        jumpAgreement() { //跳转协议
            this.$router.push({
                name: 'supplyAgreement',
                query: {
                    id: -1
                }
            })
        },
        agreeAgreement() { //勾选协议
            if(this.agreeFlag){
                this.agreeFlag = false;
                this.checkedName = 'circle';
                this.checkedClass = 'color-gray checked-icon';
            }else{
                this.agreeFlag = true;
                this.checkedName = 'checked';
                this.checkedClass = 'color-primary checked-icon';
            }
        }
    },
}
</script>

<style lang="scss" scoped>
    .supply-detail-container {
        width: 100%;
        height: 91vh;
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch;
        .project-item-info {
            width: 100%;
            margin-bottom: 10px;
            padding: 16px 16px 0;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
            background-color: #fff;
            .project-img {
                width: 80px;
                height: 80px;
                margin-right: 10px;
                border-radius: 8px;
                overflow: hidden;
                background-color: #f8f8f8;
                img {
                    width: 80px;
                    height: 80px;
                    object-fit: cover;
                }
            }
            .project-item-detail {
                flex: 1;
                li {
                    width: 100%;
                    margin-bottom: 10px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    font-size: 14px;
                    color: #969C9E;
                    &:nth-child(3),
                    &:nth-child(4) {
                        margin-bottom: 16px;
                    }
                    &:nth-child(5) {
                        margin-bottom: 0;
                    }
                    .project-name {
                        width: 250px;
                        line-height: 24px;
                        font-size: 16px;
                        color: #344047;
                        overflow : hidden;
                        display: -webkit-box;
                        -webkit-line-clamp: 2;
                        -webkit-box-orient: vertical;
                    }
                    .project-price {
                        span {
                            position: relative;
                            top: 1px;
                            margin-right: 6px;
                            color: #F58523;
                            font-size: 16px;
                        }
                    }
                    .project-status {
                        text-align: right;
                        font-size: 12px;
                    }
                }
            }
        }
        .supply-goods-box {
            width: 100%;
            margin-bottom: 10px;
            .supply-goods-top {
                width: 100%;
                margin-bottom: 10px;
                background-color: #fff;
                li {
                    width: 100%;
                    padding: 16px;
                    box-sizing: border-box;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    border-bottom: 1px solid #f2f2f2;
                    color: #5D6164;
                    &:first-child {
                        font-size: 16px;
                        font-weight: 500;
                    }
                    &:last-child {
                        border-bottom: none;
                    }
                    p {
                        &:first-child {
                            width: 90px;
                        }
                    }
                    p {
                        &:last-child {
                            flex: 1;
                        }
                    }
                }
                .supply-goods-top-item {
                    p {
                        &:first-child {
                            font-size: 16px;
                            font-weight: 400;
                            color: #344047;
                        }
                    }
                    .supply-goods-choose {
                        flex: 1;
                        text-align: right;
                    }
                }
            }
            .supply-goods-bottom {
                width: 100%;
                background-color: #fff;
                .supply-goods-bottom-title {
                    width: 100%;
                    padding: 16px;
                    box-sizing: border-box;
                    font-size: 16px;
                    font-weight: 500;
                    color: #344047;
                    border-bottom: 1px solid #f2f2f2;
                }
                .supply-goods-list {
                    width: 100%;
                    padding: 0 16px;
                    box-sizing: border-box;
                    .supply-goods-list-top {
                        width: 100%;
                        padding-top: 16px;
                        box-sizing: border-box;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        li {
                            color: #969C9E;
                            &:nth-child(1) {
                                width: 110px;
                                padding-left: 25px;
                                box-sizing: border-box;
                            }
                            &:nth-child(2) {
                                width: 150px;
                                text-align: center;
                            }
                            &:nth-child(3) {
                                flex: 1;
                                padding-left: 18px;
                                box-sizing: border-box;
                            }
                        }
                    }
                    .supply-goods-list-bottom {
                        width: 100%;
                        padding-bottom: 18px;
                        box-sizing: border-box;
                        li {
                            width: 100%;
                            display: flex;
                            justify-content: space-between;
                            align-items: center;
                            margin-top: 18px;
                            color: #5D6164;
                            .goods-name {
                                width: 100px;
                                line-height: 20px;
                            }
                            .goods-no {
                                width: 150px;
                                word-wrap: break-word;
                                text-align: center;
                                // font-size: 13px;
                            }
                            .goods-quantity {
                                width: 40px;
                                text-align: center;
                            }
                            .goods-checked {
                                width: 18px;
                                height: 18px;
                                img {
                                    position: relative;
                                    top: -1px;
                                    width: 18px;
                                    height: 18px;
                                }
                            }
                        }
                    }
                }
                .no-data-text {
                    padding: 20px 0;
                    box-sizing: border-box;
                    text-align: center;
                    color: #969C9E;
                }
            }
        }

        .supply-agreement-box {
            width: 100%;
            margin-bottom: 20px;
            padding: 24px 16px 16px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            background-color: #fff;
            .checked-icon {
                position: relative;
                top: -1px;
                margin-right: 8px;
                font-size: 20px;
            }
            p {
                font-size: 12px;
                color: #5D6164;
            }
        }
    }

    .supply-detail-bottom {
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 9;
        width: 100%;
        height: 63px;
        padding: 0 16px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
        border-top: 1px solid #f2f2f2;
        .supply-detail-bottom-left {
            flex: 1;
            span {
                font-size: 16px;
                font-weight: 500;
                color: #F58523;
                &:first-child {
                    margin-right: 16px;
                }
            }
        }
        .sure-btn {
            width: 144px;
            height: 51px;
            line-height: 51px;
            font-size: 18px;
            box-shadow: none;
        }
    }
    .bottom-info {
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 9;
        width: 100%;
        height: 63px;
        padding-right: 16px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
        border-top: 1px solid #f2f2f2;
        .bottom-info-left {
            flex: 1;
            display: flex;
            flex-direction: column;
            text-align: center;
            color: #969C9E;
            .phone-icon {
                font-size: 24px;
            }
            span {
                margin-top: 8px;
                font-size: 12px;
            }
        }
        .sure-btn {
            width: 230px;
            height: 51px;
            line-height: 51px;
            font-size: 18px;
            box-shadow: none;
        }
    }
</style>